import {
  Controller,
  Get,
  Post,
  Body,
  Patch,
  Param,
  Query,
  Res,
  Req
} from '@nestjs/common';
import { RoleService } from './role.service';
import { CreateRoleDto } from './dto/create-role.dto';
import { UpdateRoleDto } from './dto/update-role.dto';
import { QueryRoleDto } from './dto/query-role.dto';
import { DeleteRoleDto } from './dto/delete-role.dto';
import { ApiTags, ApiOperation, ApiBearerAuth } from '@nestjs/swagger';
import { Response,Request } from 'express';
import { StatusChangeDto } from 'src/common/baseDto/status-change.dto';
import { RequirePermission } from 'src/decorator/reuqire-permission';

@ApiTags('角色管理')
@Controller('role')
@ApiBearerAuth()
export class RoleController {
  constructor(private readonly roleService: RoleService) {}

  @Post()
  @ApiOperation({ summary: '创建角色' })
  @RequirePermission("system:role:add")
  create(@Body() createRoleDto: CreateRoleDto) {
    return this.roleService.create(createRoleDto);
  }

  @Get()
  @ApiOperation({ summary: '角色列表' })
  @RequirePermission("system:role:list")
  findAll(@Query() query: QueryRoleDto) {
    return this.roleService.findAll(query);
  }

  @Get(':id')
  @ApiOperation({ summary: '角色详情' })
  findOne(@Param('id') id: string) {
    return this.roleService.findOne(+id);
  }

  @Patch(':id')
  @ApiOperation({ summary: '修改角色' })
  @RequirePermission("system:role:edit")
  update(@Param('id') id: string, @Body() updateRoleDto: UpdateRoleDto,@Req() req:Request) {
    return this.roleService.update(+id, updateRoleDto,req);
  }

  @Post('statusChange')
  @ApiOperation({ summary: '更新角色状态' })
  @RequirePermission("system:role:edit")
  statusChange(@Body() statusChangeDto: StatusChangeDto,@Req() req:Request) {
    return this.roleService.statusChange(statusChangeDto,req);
  }

  @Post('delete')
  @ApiOperation({ summary: '删除角色' })
  @RequirePermission("system:role:remove")
  remove(@Body() dto: DeleteRoleDto) {
    return this.roleService.remove(dto.roleIds);
  }

  @Post('excel')
  @ApiOperation({ summary: '导出角色' })
  @RequirePermission("system:role:export")
  exportToExcel(@Res() res: Response) {
    return this.roleService.exportToExcel(res);
  }
}
